IF EXISTS (SELECT * FROM sysobjects WHERE name = 'InsMachineDriveVolumeSpace' AND type = 'P')
     DROP PROCEDURE dbo.InsMachineDriveVolumeSpace
GO

CREATE PROCEDURE dbo.InsMachineDriveVolumeSpace
     @MachineId                int,
     @PackageExecutionId       bigint,
     @MachineDriveVolumeId     int,
     @VolumeDeviceId           varchar(128),
     @VolumeCapacity           bigint,
     @VolumeFreeSpace          bigint,
     @PackageExecutionIdStr    varchar(30)   = NULL
AS
/*
 *
 * InsMachineDriveVolumeSpace
 *
 * Purpose:      
 *
 * Inputs:       
 *
 * Assumes:      
 *
 * Effects:      
 *
 * Returns:      
 *
 * Note(s):      
 *
 * CfgCtrl Keys: None
 *
 * Author(s):    NOVA Concepts Ltd.
 *
 * Change Log:   Initial Release, 06/02/2011, 1.00.0000
 *
 */
BEGIN
     SET NOCOUNT ON
     SET XACT_ABORT ON

     -- Early bail point, for cases where the capacity is NULL, we have a drive 
     -- but the OS does not know its size, so we record the drive but not its
     -- capacity or available space.  We bail here before we do any additional 
     -- processing because we know the record won't be inserted later.
     IF ( @VolumeCapacity IS NULL )
     BEGIN
          RETURN 0
     END

     DECLARE @MinTime        int,
             @VolumeIdForSrv int,
             @PEI            bigint,
             @RC             int,
             @NOW            datetime

     -- Get current date/time
     SELECT @NOW = CURRENT_TIMESTAMP 
     
     -- Handle case of PackageExecutionId
     IF ( @PackageExecutionId IS NULL AND @PackageExecutionIdStr IS NOT NULL )
     BEGIN -- Id value ALWAYS takes precedence
          SELECT @PackageExecutionId = CONVERT( bigint, @PackageExecutionIdStr )
     END
     ELSE 
     BEGIN -- All other cases we set the string to match the id
          SELECT @PackageExecutionIdStr = CONVERT( varchar(30), @PackageExecutionId )
     END

     -- We may have the DeviceId or the direct id to the volume, if we have the 
     -- volume id it takes precedence.  If not we need to look it up.
     IF ( @MachineDriveVolumeId IS NULL AND @VolumeDeviceId IS NOT NULL )
     BEGIN -- We need to calc the volume id for the server instance
          SELECT @VolumeIdForSrv = MachineDriveVolumeId 
          FROM   dbo.MachineDriveVolume
          WHERE  MachineId      = @MachineId 
          AND    VolumeDeviceId = @VolumeDeviceId
     END
     ELSE
     BEGIN -- We use a little trick here and assume something was passed, we'll 
           -- make sure in a bit.
          SET @VolumeIdForSrv = @MachineDriveVolumeId
     END

     IF ( @VolumeIdForSrv IS NULL )
     BEGIN
          EXEC dbo.InsEventLog 
                    @EventLogTypeId    = 3, -- Error
                    @LogByApp          = 'InsMachineDriveVolumeSpace',
                    @LogByAppSubSystem = NULL,
                    @LogByDesc         = NULL,
                    @LogText           = 'Could not determine the server instance volume id from the passed values, record insertion failed!',
                    @MiscVC1           = @VolumeDeviceId,
                    @MiscInt1          = @MachineId,
                    @MiscInt2          = @MachineDriveVolumeId,
                    @MiscBigInt1       = @PackageExecutionId,
                    @MiscBigInt2       = @VolumeCapacity,
                    @MiscBigInt3       = @VolumeFreeSpace

          RAISERROR( 'Could not determine the server instance volume id from the passed values, record insertion failed!', 16, 1 )

          RETURN 1
     END

     -- Before we do the insert we have one last check, to keep there from being
     -- too many samples we won't write a sample less than 55 minutes from the 
     -- last sample.

     -- Get the configured time
     SELECT @MinTime = ISNULL( dbo.GetCfgCtrlKeyAsInt( 'MinMinutesBetweenVolSpcSamples' ), 55 )

     IF NOT EXISTS ( SELECT 'X' 
                     FROM   dbo.MachineDriveVolumeSpace 
                     WHERE  MachineId            = @MachineId
                     AND    MachineDriveVolumeId = @VolumeIdForSrv
                     AND    CaptureDt            > DATEADD( mi, -1 * @MinTime, @NOW ) )
     BEGIN
          -- Do the insert, the used space gets calculated and stored in real-time so
          -- we don't have to handle that.  Also the two flags are not set by this
          -- process, that functionality is handled elsewhere, the purpose here is
          -- to get the data inserted.
          INSERT dbo.MachineDriveVolumeSpace
               ( MachineId,
                 MachineDriveVolumeId,
                 CaptureDt,
                 PackageExecutionId,
                 VolumeCapacityBytes,
                 VolumeFreeSpaceBytes )
          SELECT @MachineId,
                 @VolumeIdForSrv,
                 @NOW, -- CaptureDt
                 @PackageExecutionId,
                 @VolumeCapacity,
                 @VolumeFreeSpace
     END
END
GO
